package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.business.QueueMap;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    //加载物资资料列表
    @Override
    public PageVO<ProductVO> findProductList(Map<String, Object> map) {
        PageHelper.startPage(Integer.valueOf((String) map.get("pageNum")),
                Integer.valueOf((String) map.get("pageSize")));
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();

        //选择分类查询
        String categoryIds = (String)map.get("categorys");
        if (categoryIds != null && !categoryIds.equals("")){
            String[] split = categoryIds.split(",");
            if (split[0] != null && !"".equals(split[0])){
                criteria.andEqualTo("oneCategoryId",Integer.valueOf(split[0]));
            }
            if (split[1] != null && !"".equals(split[1])){
                criteria.andEqualTo("twoCategoryId",Integer.valueOf(split[1]));
            }
            if (split[2] != null && !"".equals(split[2])){
                criteria.andEqualTo("threeCategoryId",Integer.valueOf(split[2]));
            }
        }

        String name = (String) map.get("name");
        if (name!=null && !"".equals(name)){
            criteria.andLike("remark","%"+ name + "%");
        }

        Integer status = Integer.valueOf((String) map.get("status"));
        if (status != null){
            criteria.andEqualTo("status",status);
        }

        List<Product> productList = productMapper.selectByExample(example);
        List<ProductVO> productListVO = productList.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        PageVO<ProductVO> pageVO = new PageVO<>(pageInfo.getTotal(), productListVO);
        return pageVO;
    }

    @Override
    public void add(ProductVO productVO) {
        Date date = new Date();
        productVO.setCreateTime(date);
        productVO.setModifiedTime(date);
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.substring(0,32);
        productVO.setPNum(uuid);
        @NotNull(message = "分类不能为空") Long[] array = productVO.getCategoryKeys();
        productVO.setOneCategoryId(array[0]);
        productVO.setTwoCategoryId(array[1]);
        productVO.setThreeCategoryId(array[2]);
        productVO.setStatus(2);

        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        productMapper.insert(product);
    }

    //打开编辑物资页面
    @Override
    public ProductVO edit(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectByPrimaryKey(product);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product1,productVO);
        return productVO;
    }

    //修改物资资料
    @Override
    public void update(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void remove(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectByPrimaryKey(product);
        product1.setStatus(1);
        productMapper.updateByPrimaryKey(product1);
    }

    //把物资从回收站中恢复
    @Override
    public void back(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectByPrimaryKey(product);
        product1.setStatus(0);
        productMapper.updateByPrimaryKey(product1);
    }

    //从回收站中删除物资
    @Override
    public void delete(Long id) {
        Product product = new Product();
        product.setId(id);
        productMapper.deleteByPrimaryKey(product);
    }

    @Override
    public void publish(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectByPrimaryKey(product);
        product1.setStatus(0);
        productMapper.updateByPrimaryKey(product1);
    }

    //加载可入库的商品列表
    @Override
    public PageVO<ProductVO> findProducts(Map<String, Object> map) {
        PageHelper.startPage(Integer.valueOf(String.valueOf(map.get("pageNum"))),
                Integer.valueOf(String.valueOf(map.get("pageSize"))));
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();

        //选择分类查询
        String categoryIds = (String)map.get("categorys");
        if (categoryIds != null && !categoryIds.equals("")){
            String[] split = categoryIds.split(",");
            if (split[0] != null && !"".equals(split[0])){
                criteria.andEqualTo("oneCategoryId",Integer.valueOf(split[0]));
            }
            if (split[1] != null && !"".equals(split[1])){
                criteria.andEqualTo("twoCategoryId",Integer.valueOf(split[1]));
            }
            if (split[2] != null && !"".equals(split[2])){
                criteria.andEqualTo("threeCategoryId",Integer.valueOf(split[2]));
            }
        }

        String name = (String) map.get("name");
        if (name!=null && !"".equals(name)){
            criteria.andLike("name","%" + name + "%");
        }
        List<Product> list = productMapper.selectByExample(example);
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        List<ProductVO> listVo = list.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());

        PageVO<ProductVO> pageVO = new PageVO<ProductVO>(pageInfo.getTotal(),listVo);
        return pageVO;
    }

    //物资库存页面加载库存信息
    @Override
    public PageVO<ProductVO> findProductStocks(Map<String, Object> map) {
        PageHelper.startPage(Integer.valueOf(String.valueOf(map.get("pageNum"))),
                Integer.valueOf(String.valueOf(map.get("pageSize"))));
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();

        //选择分类查询
        String categoryIds = (String)map.get("categorys");
        if (categoryIds != null && !categoryIds.equals("")){
            String[] split = categoryIds.split(",");
            if (split[0] != null && !"".equals(split[0])){
                criteria.andEqualTo("oneCategoryId",Integer.valueOf(split[0]));
            }
            if (split[1] != null && !"".equals(split[1])){
                criteria.andEqualTo("twoCategoryId",Integer.valueOf(split[1]));
            }
            if (split[2] != null && !"".equals(split[2])){
                criteria.andEqualTo("threeCategoryId",Integer.valueOf(split[2]));
            }
        }

        String name = (String) map.get("name");
        if (name!=null && !"".equals(name)){
            criteria.andLike("name","%" + name + "%");
        }
        List<Product> list = productMapper.selectByExample(example);
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        List<ProductVO> listVo = list.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            String pNum = productVO.getPNum();
            Example e = new Example(ProductStock.class);
            Example.Criteria c = e.createCriteria();
            c.andEqualTo("pNum",pNum);
            ProductStock productStock = productStockMapper.selectOneByExample(e);
            if (productStock != null)
            productVO.setStock(productStock.getStock());

            return productVO;
        }).collect(Collectors.toList());

        PageVO<ProductVO> pageVO = new PageVO<ProductVO>(pageInfo.getTotal(),listVo);
        return pageVO;
    }

    @Autowired
    private ProductStockMapper productStockMapper;

}
